﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.Services;
using DDSC.OracleADO;
using System.Collections;
using System.Text;
using System.Data;

public partial class krj_document : BasePermissionPage
{
    public string DocumentNo { get; set; }
    public string DEPT_ID { get; set; }
    public string CATEGORY { get; set; }
    public string Mode { get; set; }
 
  protected void Page_Load(object sender, EventArgs e)
  {
      try
      {
         
          if (!Page.IsPostBack)
          {
              DataSet ds = null;
              ds = DBINT();
              if (ds != null && ds.Tables[0].Rows[0]["ReturnCode"] == "000")
              {
                  //DropDownList資料bind入
                  ddldept.DataTextField = "DEPT_NAME";
                  ddldept.DataValueField = "DEPT_ID";
                  ddldept.DataSource = ds.Tables["DEPT"];
                  ddldept.DataBind();
              }

              SDS_KRJ_DOCUMENT.SelectCommand = "SELECT * FROM TG.KRJ_DOCUMENT WHERE 1=2";
          }
      }
      catch (Exception ex)
      {
          //Response.Write(e.Message);
          //lbl_message.Text = ex.Message;
          lbl_message.Text = BP_Message;
          ERROR_DBSAVE("krj_document.aspx.cs", "Page_Load", ex.Message, "", "c#", "0", ex.StackTrace);

      }
  }
  protected void btn_exit_Click(object sender, EventArgs e)
  {
    Response.Redirect("~/Default.aspx");
  }

  protected void BindGrid()
  {
      try
      {
          SDS_KRJ_DOCUMENT.SelectParameters.Clear();

          if (!string.IsNullOrEmpty(ddldept.SelectedValue))
          {
              SDS_KRJ_DOCUMENT.SelectCommand +=
                  " AND A.DEPT_ID = :DEPT_ID ";
              SDS_KRJ_DOCUMENT.SelectParameters.Add(":DEPT_ID", ddldept.SelectedValue);
          }
          if (!string.IsNullOrEmpty(ddl_category.SelectedValue))
          {
              SDS_KRJ_DOCUMENT.SelectCommand +=
                  " AND A.DOCUMENT_CATEGORY = :DOCUMENT_CATEGORY ";
              SDS_KRJ_DOCUMENT.SelectParameters.Add(":DOCUMENT_CATEGORY", ddl_category.SelectedValue);
          }
          if (!string.IsNullOrEmpty(txt_document_no.Text))
          {
              SDS_KRJ_DOCUMENT.SelectCommand +=
                  " AND A.DOCUMENT_NO = :DOCUMENT_NO ";
              SDS_KRJ_DOCUMENT.SelectParameters.Add(":DOCUMENT_NO", "%" + txt_document_no.Text);
          }

          if (!string.IsNullOrEmpty(txt_document_name.Text))
          {
              SDS_KRJ_DOCUMENT.SelectCommand +=
                  " AND A.DOCUMENT_NAME LIKE :DOCUMENT_NAME ";
              SDS_KRJ_DOCUMENT.SelectParameters.Add(":DOCUMENT_NAME", "%" + txt_document_name.Text + "%");
          }

          SDS_KRJ_DOCUMENT.SelectCommand += " ORDER BY A.DOCUMENT_NO DESC";
          SDS_KRJ_DOCUMENT.Select(DataSourceSelectArguments.Empty);

          gv_document.DataBind();
      }
      catch (Exception ex)
      {
          lbl_message.Text = BP_Message;
          ERROR_DBSAVE("krj_document.aspx.cs", "BindGrid", ex.Message, "", "c#", "0", ex.StackTrace);
      }
  }

  protected void btn_query_Click(object sender, EventArgs e)
  {
    try
    {
      var controlItem = new List<ControlItem>() 
    {
      new ControlItem("ddl_category", ddl_category.SelectedValue, ddl_category.GetType()),
      new ControlItem("txt_document_no", txt_document_no.Text, txt_document_no.GetType()),
      new ControlItem("txt_document_name", txt_document_name.Text, txt_document_name.GetType())      
    };
      ConditionRecordUtil.RecordSearchConditionData(controlItem);
    }
    catch (Exception ex)
    {
      CommonUtil.DisplayInfoMessage(ex.Message, lbl_message);
      ERROR_DBSAVE("krj_document.aspx.cs", "btn_query_Click", ex.Message, "", "c#", "0", ex.StackTrace);
    }

    BindGrid();
    if (gv_document.Rows.Count == 0)
    {
      CommonUtil.DisplayErrorMessage("查無資料", lbl_message);

    }    
  }

 
  protected void btn_insert_Click(object sender, EventArgs e)
  {
    Mode = "Insert";
    Server.Transfer("~/Management/Forms/krj_document_detail.aspx", false);
  }
  protected void gv_document_RowCommand(object sender, GridViewCommandEventArgs e)
  {
    if (e.CommandName == "Select")
    {
      Button btn = e.CommandSource as Button;
      switch (btn.ID)
      {
        case "btn_view":
          Mode = "View";
          break;
        case "btn_edit":
          Mode = "Update";
          break;
        case "btn_delete":
          Mode = "Delete";
          break;
      }
    }   
  }
  
  protected void gv_document_SelectedIndexChanged(object sender, EventArgs e)
  {
      Label lbl_DEPT_ID = gv_document.SelectedRow.FindControl("lbl_DEPT_ID") as Label;
      Label lbl_CATEGORY = gv_document.SelectedRow.FindControl("lbl_DOCUMENT_CATEGORY") as Label;
    Label lbl_DOCUMENT_NO = gv_document.SelectedRow.FindControl("lbl_DOCUMENT_NO") as Label;
    DocumentNo = lbl_DOCUMENT_NO.Text;
    DEPT_ID = lbl_DEPT_ID.Text;
    CATEGORY = lbl_CATEGORY.Text;
    Server.Transfer("~/Management/Forms/krj_document_detail.aspx", false);
  }
  protected void gv_document_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    gv_document.PageIndex = e.NewPageIndex;
    BindGrid();
  }
  protected DataSet DBINT()
  {
      DataSet ds = null;
      try
      {
          dbAccess m_dba = new dbAccess();
          StringBuilder m_sb = new StringBuilder();
          ArrayList m_arrParam = new ArrayList();
          m_sb.Append(" select distinct A.DEPT_ID , A.DEPT_ID || ' ' || B.DEPT_NAME DEPT_NAME ");
          m_sb.Append(" FROM KRJ_CATEGORY_DOCUMENT A left join mis_dept_ap B on A.DEPT_ID = B.DEPT_ID  ");
          m_sb.Append(" order by A.dept_id ");
          ds = m_dba.runExecuteQuery("DEPT", m_sb.ToString(), m_arrParam);
          

      }
      catch (Exception ex)
      {
          lbl_message.Text = BP_Message;
          ERROR_DBSAVE("krj_document.aspx.cs", "DBINT", ex.Message, "", "c#", "0", ex.StackTrace);
      }
      return ds;
  }
}